var PageCallback=null
var patchPage={
    PageJslist:[],
    loadReady:function(callback){
        PageCallback=callback;
        this.execute();
    },
    loadJS:function(src){
        this.PageJslist.push(src)
    },
    execute:function(){
        if( this.PageJslist.length==0){
            if(PageCallback)
            PageCallback()  
        }else{
            var jsString= this.PageJslist[0];
            that=this;
            try{
                patchJS(jsString,function(){
                    remove(that.PageJslist,jsString)
                    that.execute()
                })
            }catch(e){
                console.log(e)
                remove(that.PageJslist,jsString)
                that.execute()
            }
           
        }
    }
    

}
function remove (arr,val) {
    var index = arr.indexOf(val);
    if (index > -1) {
        arr.splice(index, 1);
    }
}
 // 行内方式动态加载js代码
  function loadJSText(jsText){
    var script = document.createElement('script');
    script.type = 'text/javascript';
    try {
        // Firefox,Safari,Chrome,Opera支持
        script.appendChild(document.createTextNode(jsText));
    } catch(ex){
        // IE早期的浏览器，需要使用script的text属性来指定js代码
        script.text = jsText;
    }
    document.body.appendChild(script);
}
// 动态加载外部CSS文件
function loadCSS(url){
    var link = document.createElement('link');
    link.rel = 'stylesheet';
    link.type = 'text/css';
    link.url = url;
    document.getElementsByTagName('head')[0].appendChild(link);
}
function patchJS(fileName, callback) {
        

    callback      = callback || function() {};
    
    var script    = null; 
    script        = document.createElement("script");
    script.id     = fileName.replace(/[\./]+/g, "-");
    script.type   = "text/javascript";        
    script.src    = fileName;
    
    if (editormd.isIE8) 
    {            
        script.onreadystatechange = function() {
            if(script.readyState) 
            {
                if (script.readyState === "loaded" || script.readyState === "complete") 
                {
                    script.onreadystatechange = null; 
                    editormd.loadFiles.js.push(fileName);
                    callback();
                }
            } 
        };
    }
    else
    {
        script.onload = function() {
            editormd.loadFiles.js.push(fileName);
            callback();
        };
    }
        document.body.appendChild(script);
}
